import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Test_WindowListener {
    public static void main(String[] args) {
        new WindowFrame();
    }
}
class  WindowFrame extends Frame{
    public WindowFrame() throws HeadlessException {
        setBackground(Color.GRAY);
        setBounds(300,300,400,300);
        setVisible(true);
        addWindowListener(new MyindowListener());//添加监听

    }
    class MyindowListener extends WindowAdapter{
        @Override
        public void windowClosing(WindowEvent e) {
            setVisible(false);//隐藏窗口
            System.exit(0);//正常退出程序
        }
    }
}